unit uFrmClockControl;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, uFrmDisplay, ComCtrls, ExtDlgs, inifiles, Grids, strutils;

type
  TFrmClockControl = class(TForm)
    PageControl1: TPageControl;
    General: TTabSheet;
    btn_Pause: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    Visual: TTabSheet;
    Button9: TButton;
    CBX_TournamentData: TColorBox;
    CBX_ThisLevel: TColorBox;
    CBX_GeneralData: TColorBox;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Blinds: TTabSheet;
    stg_Blinds: TStringGrid;
    Label6: TLabel;
    dtp_Clock: TDateTimePicker;
    Label1: TLabel;
    Button10: TButton;
    Panel1: TPanel;
    edt_Rebuys: TLabeledEdit;
    Button1: TButton;
    Button4: TButton;
    edt_PlayerPlaying: TLabeledEdit;
    Button5: TButton;
    Button8: TButton;
    Button7: TButton;
    edt_Addons: TLabeledEdit;
    Button2: TButton;
    Label7: TLabel;
    Panel2: TPanel;
    Label8: TLabel;
    edt_AddonStack: TLabeledEdit;
    edt_Stack: TLabeledEdit;
    edt_Buyin: TLabeledEdit;
    edt_TournamentName: TLabeledEdit;
    pnl_WinPercents: TPanel;
    lbl_TotalWinPercent: TLabel;
    Label2: TLabel;
    btn_About: TButton;
    Button12: TButton;
    Button11: TButton;
    Panel3: TPanel;
    Label9: TLabel;
    edt_PlayerBuys: TLabeledEdit;
    Button6: TButton;
    Button3: TButton;
    Button13: TButton;
    Button14: TButton;
    Label10: TLabel;
    Button15: TButton;
    Button16: TButton;
    stg_Win: TStringGrid;
    Label11: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btn_PauseClick(Sender: TObject);
    procedure dtp_ClockChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure CBX_TournamentDataChange(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure stg_BlindsSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure btn_AboutClick(Sender: TObject);
    procedure stg_BlindsDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure Button13Click(Sender: TObject);
    procedure Button14Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure stg_WinSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure stg_WinSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
  private
    CanRefresh: Boolean;
    procedure RefreshData;
    function CalcPrizePercWin(WPerc: string): string;
    { Private declarations }
  public
    function DisplayLevelsSA(LevelIdx: Integer): Boolean;
    { Public declarations }
  end;

var
  FrmClockControl: TFrmClockControl;

implementation

uses uMainUtils;
{$R *.dfm}

procedure TFrmClockControl.btn_AboutClick(Sender: TObject);
begin
  ShowMessage('LPM' + #13#10 + 'www.livepokermanager.com');
end;

procedure TFrmClockControl.btn_PauseClick(Sender: TObject);
begin
  if FrmDisplay.FrmClock.ClockPaused then
  begin
    FrmDisplay.FrmClock.ClockPaused := false;
    btn_Pause.Caption := 'PAUSE CLOCK';
  end
  else
  begin
    FrmDisplay.FrmClock.ClockPaused := true;
    btn_Pause.Caption := 'CONTINUE CLOCK';
  end;
end;

procedure TFrmClockControl.RefreshData;
var
  i: Integer;
  AComponent: TComponent;
  TotalChips: double;
  IniFile: TIniFile;
  Buyin, WinVal: double;
begin
  FrmDisplay.FrmClock.TournamemntName := edt_TournamentName.Text;
  FrmDisplay.FrmClock.RebuysAllowedValue := edt_Rebuys.Text;
  FrmDisplay.FrmClock.AddonsAllowedValue := edt_Addons.Text;
  FrmDisplay.FrmClock.PrizeMoneyValue := FloatToStr(StrToFloat(edt_Addons.Text) * StrToFloat(edt_Buyin.Text) + StrToFloat(edt_PlayerBuys.Text) * StrToFloat(edt_Buyin.Text) + StrToFloat
      (edt_Rebuys.Text) * StrToFloat(edt_Buyin.Text));
  FrmDisplay.FrmClock.PlayersValue := edt_PlayerPlaying.Text + '/' + edt_PlayerBuys.Text;
  TotalChips := StrToFloat(edt_Stack.Text) * (StrToFloat(edt_PlayerBuys.Text) + StrToFloat(edt_Rebuys.Text)) + (StrToFloat(edt_AddonStack.Text) * StrToFloat(edt_Addons.Text));
  FrmDisplay.FrmClock.TotalChipsValue := FloatToStrF(TotalChips, ffNumber, 20, 0);
  if edt_PlayerPlaying.Text <> '0' then
    FrmDisplay.FrmClock.AvgStackValue := FloatToStrF((TotalChips / StrToFloat(edt_PlayerPlaying.Text)), ffNumber, 20, 0);
  for i := 0 to 10 do
  begin
    FrmDisplay.FrmClock.stg_Prizes.Cells[1, i] := '';
    FrmDisplay.FrmClock.stg_Prizes.Cells[0, i] := '';
    FrmDisplay.FrmClock.stg_Prizes.RowCount := 0;
    FrmDisplay.FrmClock.PaidTop := '';
  end;
  // ------
  for i := 0 to 10 do
  begin
    if (stg_Win.Cells[0, i] = '0') or (stg_Win.Cells[0, i] = '') then
      break;
    Buyin := StrToFloat(edt_Buyin.Text);
    WinVal := StrToFloat(stg_Win.Cells[0, i]);
    FrmDisplay.FrmClock.stg_Prizes.Cells[1, i] := FloatToStr((Buyin * strtofloat(edt_PlayerPlaying.Text)+(Buyin * strtofloat(edt_Rebuys.Text)+(Buyin * strtofloat(edt_Addons.Text)))) / 100 * WinVal) + ' ' + CurrencyString;
    FrmDisplay.FrmClock.stg_Prizes.Cells[0, i] := inttostr(i + 1);
    FrmDisplay.FrmClock.stg_Prizes.RowCount := i + 1;
    FrmDisplay.FrmClock.stg_Prizes.Row := 0;
    FrmDisplay.FrmClock.PaidTop := inttostr(i + 1);
  end;
  // save to ini
  IniFile := TIniFile.Create(ChangeFileExt(application.ExeName, '.ini'));
  IniFile.WriteString('SASettings', 'TournamemntName', edt_TournamentName.Text);
  IniFile.WriteString('SASettings', 'RebuysAllowedValue', edt_Rebuys.Text);
  IniFile.WriteString('SASettings', 'AddonsAllowedValue', edt_Addons.Text);
  IniFile.WriteString('SASettings', 'Buyin', edt_Buyin.Text);
  IniFile.WriteString('SASettings', 'edt_AddonStack', edt_AddonStack.Text);
  IniFile.WriteString('SASettings', 'edt_Stack', edt_Stack.Text);
  IniFile.WriteString('SASettings', 'edt_PlayerPlaying', edt_PlayerPlaying.Text);
  IniFile.WriteString('SASettings', 'edt_PlayerBuys', edt_PlayerBuys.Text);
  IniFile.Free;
end;

procedure TFrmClockControl.stg_BlindsDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  s: string;
begin
  s := TStringGrid(Sender).Cells[ACol, ARow];
  if ARow = 0 then
  begin
    TStringGrid(Sender).canvas.Brush.Color := clGray;
  end
  else
  begin
    if State <> [gdSelected] then
    begin
      TStringGrid(Sender).canvas.Brush.Color := clblack
    end
    else
    begin
      TStringGrid(Sender).canvas.Brush.Color := clSkyBlue;
    end;
  end;
  TStringGrid(Sender).canvas.fillrect(Rect);
  DrawText((Sender as TStringGrid).canvas.handle, Pchar(s), Length(s), Rect, dt_wordbreak or DT_CENTER);
end;

procedure TFrmClockControl.stg_BlindsSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  IniFile: TIniFile;
  i: Integer;
begin
  IniFile := TIniFile.Create(ChangeFileExt(application.ExeName, '.ini'));
  for i := 1 to stg_Blinds.RowCount - 1 do
  begin
    IniFile.WriteString('SASettings', 'stg_BlindsSmall' + inttostr(i), stg_Blinds.Cells[0, i]);
    IniFile.WriteString('SASettings', 'stg_BlindsBig' + inttostr(i), stg_Blinds.Cells[1, i]);
    IniFile.WriteString('SASettings', 'stg_BlindsAnte' + inttostr(i), stg_Blinds.Cells[2, i]);
  end;
  IniFile.Free;
end;

procedure TFrmClockControl.stg_WinSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  IniFile: TIniFile;
  i: Integer;
begin
  IniFile := TIniFile.Create(ChangeFileExt(application.ExeName, '.ini'));
  for i := 0 to 9 do
  begin
    IniFile.WriteString('SASettings', 'stg_Win' + inttostr(i), stg_Win.Cells[0, i]);
  end;
  IniFile.Free;
end;

procedure TFrmClockControl.stg_WinSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
var
  TotPerc: double;
  i: Integer;
begin
  TotPerc := 0;
  for i := 0 to stg_Win.RowCount do
  begin
    if stg_Win.Cells[0, i] <> '' then
    begin
      TotPerc := TotPerc + StrToFloat(stg_Win.Cells[0, i]);
    end;
  end;
  lbl_TotalWinPercent.Caption := FloatToStr(TotPerc) + ' %';
  if TotPerc = 100 then
    lbl_TotalWinPercent.Font.Color := cllime
  else
    lbl_TotalWinPercent.Font.Color := clred;
  dtp_ClockChange(self);
end;

procedure TFrmClockControl.Button10Click(Sender: TObject);
begin
  FrmDisplay.FrmClock.Clock := dtp_Clock.Time;
  FrmDisplay.FrmClock.ClockSA := dtp_Clock.Time;
  btn_Pause.Click;
end;

procedure TFrmClockControl.Button11Click(Sender: TObject);
begin
  if FrmDisplay.FrmClock.RoundLevelSA > 1 then
  BEGIN
    FrmDisplay.FrmClock.RoundLevelSA := FrmDisplay.FrmClock.RoundLevelSA - 2;
    FrmDisplay.FrmClock.Clock := 0.000001;
  END;
end;

procedure TFrmClockControl.Button12Click(Sender: TObject);
begin
  FrmDisplay.FrmClock.RoundLevelSA := FrmDisplay.FrmClock.RoundLevelSA;
  FrmDisplay.FrmClock.Clock := 0.000001;
end;

procedure TFrmClockControl.Button13Click(Sender: TObject);
begin
  FrmDisplay.FrmClock.Clock := FrmDisplay.FrmClock.Clock + 0.0005;
end;

procedure TFrmClockControl.Button14Click(Sender: TObject);
begin
  FrmDisplay.FrmClock.Clock := FrmDisplay.FrmClock.Clock - 0.0005;
end;

procedure TFrmClockControl.Button15Click(Sender: TObject);
var
  AComponent: TComponent;
  i: Integer;
begin
  for i := 0 to self.ComponentCount - 1 do
  begin
    if self.Components[i] is TLabeledEdit then
    begin
      TLabeledEdit(self.Components[i]).Text := '0';
    end;
  end;
end;

procedure TFrmClockControl.Button16Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to stg_Blinds.RowCount - 1 do
  begin
    stg_Blinds.Cells[0, i] := '';
    stg_Blinds.Cells[1, i] := '';
    stg_Blinds.Cells[2, i] := '';
  end;
end;

procedure TFrmClockControl.Button1Click(Sender: TObject);
begin
  edt_Rebuys.Text := inttostr(strtoint(edt_Rebuys.Text) + 1);
end;

procedure TFrmClockControl.Button2Click(Sender: TObject);
begin
  edt_Addons.Text := inttostr(strtoint(edt_Addons.Text) + 1);
end;

procedure TFrmClockControl.Button3Click(Sender: TObject);
begin
  edt_PlayerBuys.Text := inttostr(strtoint(edt_PlayerBuys.Text) + 1);
  edt_PlayerPlaying.Text := inttostr(strtoint(edt_PlayerPlaying.Text) + 1);
end;

procedure TFrmClockControl.Button4Click(Sender: TObject);
begin
  if strtoint(edt_PlayerBuys.Text) > strtoint(edt_PlayerPlaying.Text) then
  begin
    edt_PlayerPlaying.Text := inttostr(strtoint(edt_PlayerPlaying.Text) + 1);
  end;
end;

procedure TFrmClockControl.Button5Click(Sender: TObject);
begin
  if strtoint(edt_PlayerPlaying.Text) > 0 then
    edt_PlayerPlaying.Text := inttostr(strtoint(edt_PlayerPlaying.Text) - 1);
end;

procedure TFrmClockControl.Button6Click(Sender: TObject);
begin
  if strtoint(edt_PlayerBuys.Text) > 0 then
  begin
    edt_PlayerBuys.Text := inttostr(strtoint(edt_PlayerBuys.Text) - 1);
    if strtoint(edt_PlayerPlaying.Text) > 0 then
    begin
      edt_PlayerPlaying.Text := inttostr(strtoint(edt_PlayerPlaying.Text) - 1);
    end;
  end;
end;

procedure TFrmClockControl.Button7Click(Sender: TObject);
begin
  if strtoint(edt_Addons.Text) > 0 then
    edt_Addons.Text := inttostr(strtoint(edt_Addons.Text) - 1);
end;

procedure TFrmClockControl.Button8Click(Sender: TObject);
begin
  if strtoint(edt_Rebuys.Text) > 0 then
    edt_Rebuys.Text := inttostr(strtoint(edt_Rebuys.Text) - 1);
end;

procedure TFrmClockControl.Button9Click(Sender: TObject);
var
  Filename: string;
  IniFile: TIniFile;
begin
  if (OpenPictureDialog1.Execute()) then
  begin
    FrmDisplay.FrmClock.img_Background.picture.LoadFromFile(OpenPictureDialog1.Filename);
    // ---
    IniFile := TIniFile.Create(ChangeFileExt(application.ExeName, '.ini'));
    IniFile.WriteString('Other', 'BackGroundPic', OpenPictureDialog1.Filename);
    IniFile.Free;
  end;
end;

function TFrmClockControl.CalcPrizePercWin(WPerc: string): string;
begin
  result := FloatToStr(StrToFloat(FrmDisplay.FrmClock.PrizeMoneyValue) * StrToFloat(WPerc) / 100);
end;

procedure TFrmClockControl.CBX_TournamentDataChange(Sender: TObject);
begin
  FrmDisplay.FrmClock.SetFontColors(CBX_TournamentData.Selected, CBX_ThisLevel.Selected, CBX_GeneralData.Selected);
end;

function TFrmClockControl.DisplayLevelsSA(LevelIdx: Integer): Boolean;
begin
  FrmDisplay.FrmClock.LevelName := inttostr(LevelIdx);
  FrmDisplay.FrmClock.Blinds := stg_Blinds.Cells[0, LevelIdx] + '/' + stg_Blinds.Cells[1, LevelIdx];
  FrmDisplay.FrmClock.Ante := stg_Blinds.Cells[2, LevelIdx];
  // ----
  if stg_Blinds.Cells[0, LevelIdx + 1] <> '' then
  begin
    FrmDisplay.FrmClock.NextLevelName := inttostr(LevelIdx + 1);
    FrmDisplay.FrmClock.NextBlinds := stg_Blinds.Cells[0, LevelIdx + 1] + '/' + stg_Blinds.Cells[1, LevelIdx + 1];
    FrmDisplay.FrmClock.NextAnte := stg_Blinds.Cells[2, LevelIdx];
    result := true;
  end
  else
  begin
    FrmDisplay.FrmClock.NextLevelName := 'FINE';
    FrmDisplay.FrmClock.NextBlinds := '';
    FrmDisplay.FrmClock.NextAnte := '';
    result := false;
  end;
end;

procedure TFrmClockControl.dtp_ClockChange(Sender: TObject);
begin
  if CanRefresh then
    RefreshData;
end;

procedure TFrmClockControl.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FrmDisplay.Close;
end;

procedure TFrmClockControl.FormCreate(Sender: TObject);
var
  IniFile: TIniFile;
  i: Integer;
begin
  // -----
  CanRefresh := false;
  IniFile := TIniFile.Create(ChangeFileExt(application.ExeName, '.ini'));
  edt_TournamentName.Text := IniFile.ReadString('SASettings', 'TournamemntName', '');
  edt_Rebuys.Text := IniFile.ReadString('SASettings', 'RebuysAllowedValue', '');
  edt_Addons.Text := IniFile.ReadString('SASettings', 'AddonsAllowedValue', '');
  edt_Buyin.Text := IniFile.ReadString('SASettings', 'Buyin', '');
  edt_AddonStack.Text := IniFile.ReadString('SASettings', 'edt_AddonStack', '');
  edt_PlayerPlaying.Text := IniFile.ReadString('SASettings', 'edt_PlayerPlaying', '');
  edt_PlayerBuys.Text := IniFile.ReadString('SASettings', 'edt_PlayerBuys', '');
  edt_Stack.Text := IniFile.ReadString('SASettings', 'edt_Stack', '0');
  for i := 1 to stg_Blinds.RowCount - 1 do
  begin
    stg_Blinds.Cells[0, i] := IniFile.ReadString('SASettings', 'stg_BlindsSmall' + inttostr(i), '');
    stg_Blinds.Cells[1, i] := IniFile.ReadString('SASettings', 'stg_BlindsBig' + inttostr(i), '');
    stg_Blinds.Cells[2, i] := IniFile.ReadString('SASettings', 'stg_BlindsAnte' + inttostr(i), '');
  end;
  for i := 0 to stg_Blinds.RowCount - 1 do
  begin
    stg_Win.Cells[0, i] := IniFile.ReadString('SASettings', 'stg_Win' + inttostr(i), '');
  end;
  IniFile.Free;
  CanRefresh := true;
  stg_WinSetEditText(self, 0, 0, stg_Blinds.Cells[0, 0]);
  RefreshData;
  // ---
  CBX_TournamentData.Selected := FrmDisplay.FrmClock.ColorTournamentData;
  CBX_ThisLevel.Selected := FrmDisplay.FrmClock.ColorThisLevel;
  CBX_GeneralData.Selected := FrmDisplay.FrmClock.ColorGeneralData;
  self.Parent := FrmDisplay.FrmClock;
  dtp_Clock.DateTime := 0;
  lbl_TotalWinPercent.Caption := '';
  // BLINDS
  stg_Blinds.Cells[0, 0] := 'Small';
  stg_Blinds.Cells[1, 0] := 'Big';
  stg_Blinds.Cells[2, 0] := 'Ante';
  PageControl1.ActivePage := General;

end;

procedure TFrmClockControl.FormShow(Sender: TObject);
begin
  left := FrmDisplay.Width - Width;
  top := 0;
  height := FrmDisplay.height;
end;

end.
